﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using ELearningSystem.UI.LearnerServiceReference;

namespace ELearningSystem.UI.Account
{
    public partial class Login : System.Web.UI.Page
    {
        LearnerServiceSoapClient userManager = new LearnerServiceSoapClient();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            messageLabel.Text = "";
        }

        protected void loginButton_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Cache.SetNoStore();
                if (Request.IsAuthenticated)
                {
                    FormsAuthentication.SignOut();
                }
                if (loginEmailTextBox.Text != "" && loginPasswordTextBox.Text != "")
                {
                    LearnerDAO anLearner = new LearnerDAO();
                    anLearner = userManager.GetLearnerWithEmail(loginEmailTextBox.Text);
                    if (Membership.ValidateUser(anLearner.Name, loginPasswordTextBox.Text))
                    {
                        FormsAuthentication.RedirectFromLoginPage(anLearner.Name,true);
                    }
                    
                }
                else
                {
                    throw new Exception("Please fill-up all the field properly!");
                }
            }
            catch (Exception exception)
            {
                messageLabel.ForeColor = Color.Red;
                messageLabel.Text = exception.Message;
            }
            
            
        }
    }
}